//Code for fshare
var positionIMG = 0
var shareImgarr = [];
var shareURL = '';
var shareContent = '';
var shareUse = true;
var shareImg = null;
var shareURLErro = [];
var sErroNum = 0;
var URLinputshareCHECKED = false;

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

jQuery(document).ready(function($) { 
	jQuery('#messageShare').bind('paste', null, function() { // code 
		setTimeout(function(){
			cutandcheck('messageShare');
			}, 500);
		});
		
	jQuery('#share_title').click(function(){
		jQuery('#share_title').hide();
		swtype('titleShare');
		});
	jQuery('#titleShare').blur(function(){
		swtype('titleShare');
		jQuery('#share_title').show();
		});
	jQuery('#share_content').click(function(){
		jQuery('#share_content').hide();
		swtypetxt('sharecontent');
		});
	jQuery('#sharecontent').blur(function(){
		swtypetxt('sharecontent');
		jQuery('#share_content').show();
		});	
	if(parseInt(discuz_uid) > 0){
		setInterval(function(){
			loadnewsprompt();	
		}, 5000);
	}
});	

function swtype(id){
	if($(id).type == 'text'){
		var bac = $(id);
		$('share_title').innerHTML = '';
		$('share_title').innerHTML = bac.value;
		$(id).type = 'hidden';
	}else{
		$(id).type = 'text';
		$(id).focus();
	}
}

function swtypetxt(id){
	var bac = $(id).value;
	//bac = bac.replace(/\n/gi, '<br />');
	var a = '#' + id;
	if($(id).style.display != 'none'){
		jQuery(a).hide();
		$('share_content').innerHTML = '';
		$('share_content').innerHTML = bac;
	}else{
		jQuery(a).show();
		$(id).focus();
	}
}

function checkURL(event,id){
	// MSIE hack
	if (window.event)
	{
		event = window.event;
	}
	var a = $(id);
	resizeTx(a, 10);
	if((event.keyCode == 32 || event.keyCode == 13) && URLinputshareCHECKED == false){
		cutandcheck(id);
	}
}


function cutandcheck(id){
	var a = $(id);
	var txt = a.value;
		txt = txt.replace(/\n/gi, ' ');
	var arr = txt.split(" ");
	for(s in arr ){
		if(arr[s].length > 11)
			var URLCheck = arr[s];
	}
	if(!isUndefined(URLCheck)){
		check_it(URLCheck);
	}
}

function isUrl(url) { 
	var v = new RegExp(); 
	v.compile("^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$"); 
	if (!v.test(url))
		return false; 
	return true;
}

function check_it(txt){
	if (isUrl(txt) && !in_Array(txt, shareURLErro)){
		var loading_show	= document.getElementById('loading_show');
		loading_show.style.display	= '';
		jQuery('#sharesubmit_btn').hide();
		var x = new Ajax();
		x.get('home.php?mod=misc&ac=ajax&op=geturlshare&link=' + txt + '&inajax=1&rand=' + Math.random(), function(s){
			if(s.indexOf('share') != -1){
				data = eval('('+s+')')
				URLinputshareCHECKED = true;
				binddatashare(data['share']);
				jQuery('#sharesubmit_btn').show();
			}else{
				URLinputshareCHECKED = false;
				shareURLErro[sErroNum] = txt;
				sErroNum++;
				showError('Xin lỗi, chúng tôi không thể bóc tách dữ liệu từ trang mà bạn yêu cầu!');
				loading_show.style.display	= 'none';
				jQuery('#sharesubmit_btn').show();
				
			}
		});
	 }
}

function comperIMGShare(act){
	var maxPos = shareImgarr.length;
	
	var box_select_img 		= $('box_select_img');
	var IMG_Selected 		= $('IMG_Selected');
	var IMGHave				= $('IMGHave');
	var btn_pre				= $('btn_pre');
	var btn_next			= $('btn_next');
	var imgURLShare			= $('imgURLShare');
	var IMGCurrent			= $('IMGCurrent');
	var box_sub_txt			= $('box_sub_txt');
	var img_url_share		= $('img_url_share');

	if(maxPos == 0 && (shareImg == null || shareImg.length < 10)){
		box_sub_txt.style.display = 'none';
		img_url_share.style.display = 'none';
		//window.alert('No IMG');
	}else if(maxPos > 0 && act == ''){
		IMG_Selected.src = imgURLShare.value = shareImgarr[0];
	}else if(maxPos == 0 && act == ''){
		IMG_Selected.src = imgURLShare.value 	= shareImg;
		IMGHave.innerHTML 				= 1;
		box_select_img.style.display 	= 'none';
	}else{
		
		if(positionIMG < maxPos - 1 && act == 'next'){
			positionIMG++;
			IMG_Selected.src = imgURLShare.value  = shareImgarr[positionIMG];
		}else if(positionIMG > 0 && act == 'pre'){
			positionIMG--;
			IMG_Selected.src = imgURLShare.value  = shareImgarr[positionIMG];
		}
	}
	if(maxPos <= 1){
		box_select_img.style.display 	= 'none';
	}
	IMGCurrent.innerHTML = positionIMG+1;
	IMGHave.innerHTML 		= maxPos ? maxPos : 1;
	
	if(positionIMG == 0){
		btn_pre.className = 'poff';
	}else{
		btn_pre.className = 'pon';
	}
	
	if(positionIMG == maxPos - 1){
		btn_next.className = 'noff';
	}else{
		btn_next.className = 'non';
	}
}

function binddatashare(data){
	var box_content_share	= $('show_get');
	var loading_show		= $('loading_show');	
	var typeShare 			= $('typeShare');
	var useShare 			= $('useShare');
		
	box_content_share.style.display	= '';
	loading_show.style.display	= 'none';

	if(isUndefined(data['imgURLs'])){
		shareImg = data['imgURL'];
	}else{
		shareImgarr = [];
		for(s in data['imgURLs']){
			shareImgarr[s] = data['imgURLs'][s];
		}
	}
	comperIMGShare('');

	shareFlv = data['flv'];
	
	if(shareFlv.length > 3 && shareUse == true){
		typeShare.value = 'flv';
		useShare.value	= 'yes'
	}else{
		typeShare.value = 'text';
		useShare.value	= 'yes'
	}
	data['title'] =  data['title'] ?  data['title'] :  data['link'];
	$('share_title').innerHTML = data['title'];
	$('titleShare').value = data['title'];
	
	$('FLVShare').value = data['flv'];
	
	data['desc'] = data['desc'] ? data['desc'] : data['link'];
	$('share_content').innerHTML = data['desc'];
	$('sharecontent').value = data['desc'];
	$('URLShare').value = data['link'];
}

function notUserContentShare(){
	var box_content_share	= $('show_get');
	box_content_share.style.display = 'none';
	var useShare = $('useShare');
	useShare.value	= 'none';
}

function succeedhandle_fshare(url, msg, values){
	reset_share();
	//window.alert(url + ',' + msg + ',' + toSource(values));
}

function reset_share(){
	
	var box_content_share	= $('show_get');
	box_content_share.style.display	= 'none';
	massageS = $('messageShare');
	massageS.value = '';
	positionIMG = 0;
	shareImgarr = [];
	shareURL = '';
	shareContent = '';
	shareUse = true;
	shareImg = null;
	shareURLErro = [];
	sErroNum = 0;
	URLinputshareCHECKED = false;
	resizeTx(massageS, 23);
	$('return_fshare').style.display = 'none';
	
	loadFeedNew();
}

function userIMG(id){
	var a = $(id);
	var b = $('img_url_share');
	var c = $('imgURLShare');
	var d = $('box_select_img');

	if(a.checked){
		b.style.display = 'none';
		c.value = '';
		d.style.display = 'none';
	}else{
		b.style.display = '';
		d.style.display = '';
		c.value = $('IMG_Selected').src;
	}
}

function switchf(id){
	var fshare = $('fshare');
	var fphoto = $('fphoto');
	if(id == 'fshare'){
		fshare.style.display = '';
		fphoto.style.display = 'none';
	}else{
		fshare.style.display = 'none';
		fphoto.style.display = '';
	}
}
//end fshare

// Code for popup_page
var stateObj = { foo: "bar" };
var pop_autoloadcomment = null;

function change_my_url(page, url){
	if(BROWSER.ie){
		return false;
		url = '#!' + url;
	}
   	history.pushState(stateObj, page, url);
}

function pop_close(){
	$('popup_document_content').innerHTML = '';
	$('popup_document').style.display = 'none';
	
	if(!isUndefined($('space'))){
		$('space').style.overflow = "auto";
	}
	pop_autoloadcomment = window.clearInterval(pop_autoloadcomment);
	change_my_url('page 1',curentLocation);
}

function pic_onmouseover(id){
	var abse = $(id);
	abse.style.display = '';
}

function pic_onmouseout(id){
	var abse = $(id);
	abse.style.display = 'none';
}

function pop_pic_autoload(){
	return setInterval(function(){comment_autoload('comment_ol_popup_comment', 'pop_pic');}, 10000);
}
	
var pop_autoloadcomment = null;
function show_pic(src, fid){
	var pid = src.slice(src.indexOf("picid=") + 6);
	change_my_url('page 2',src);
	var subsrc = '&inajax=1&feedid='+fid+'&rand=' + Math.random();
	src = src + subsrc;
	showid = $('popup_document')
	var cShow = $('popup_document_content');
	cShow.innerHTML = "";
	if(!isUndefined($('space'))){
		$('space').style.overflow = "hidden";
	}
	$('loading_load').style.display = '';
	showid.style.display = '';
	showid.style.position = 'fixed';
	showid.style.margin = 'auto';
	showid.style.textAlign = 'center';
	showid.style.overflow = 'auto';
	showid.style.top =0;
	showid.style.overflowX = "hidden";
	cShow.style.margin = 'auto';
	cShow.style.textAlign = 'center';
	var x = new Ajax();
	x.get(src, function(s){
		s=evalscript(s);
		cShow.innerHTML = s;
		$('loading_load').style.display = 'none';
	});
	showid.appendChild(cShow);
	showid.style.display = '';
	pop_autoloadcomment = window.clearInterval(pop_autoloadcomment);
	pop_autoloadcomment = pop_pic_autoload();
}
// end Code for popup_page
//jQuery(document).ready(function(){cutTextShare()});